# set -e
date

env="uat"
if [ "$1" != "" ]; then env=$1; fi

PACKAGE_NAME=data-engine-0.2.0
PACKAGE_PREFIX=release/${PACKAGE_NAME}
rm -rf ${PACKAGE_PREFIX}-bin && mkdir -p ${PACKAGE_PREFIX}-bin
rm -rf ${PACKAGE_PREFIX}-bin.tgz

# find . -name '*.jar' -print -exec rm -rf {} \;

RAW_RELEASE_PATH=target/release
RELEASE_PATH=target/x86_64-unknown-linux-musl/release
UBERJAR=jar-with-dependencies.jar

echo " [*] ======== packaging date-tool ========"
mkdir -p ${PACKAGE_PREFIX}-bin/data-tool

echo " [*] >>-------- packaging data-tool/db-tool --------"
  bash -c "cd data-tool/db-tool/db-setup-rust && cargo build --release --target=x86_64-unknown-linux-musl"
  cp ${RELEASE_PATH}/db-setup-rust  ${PACKAGE_PREFIX}-bin/data-tool/db-tool.db-setup-rust

echo " [*] ======== packaging date-api ========"
mkdir -p ${PACKAGE_PREFIX}-bin/data-api

echo " [*] >>-------- packaging data-api/api-gateway --------"
echo " [*] 1. package date-api/api-gateway.push-gateway-rust ..."
  bash -c "cd data-api/api-gateway/push-gateway-rust && cargo build --release"
  cp ${RAW_RELEASE_PATH}/push-gateway-rust  ${PACKAGE_PREFIX}-bin/data-api/api-gateway.push-gateway-rust
echo " [*] 2. package date-api/api-gateway.api-gateway-rust ..."
  bash -c "cd data-api/api-gateway/api-gateway-rust && cargo build --release --target=x86_64-unknown-linux-musl"
  cp ${RELEASE_PATH}/api-gateway-rust  ${PACKAGE_PREFIX}-bin/data-api/api-gateway.api-gateway-rust

echo " [*] >>-------- packaging data-api/api-infra --------"
echo " [*] 1. package date-api/api-infra.signature-server-rust ..."
  bash -c "cd data-api/api-infra/signature-server-rust && cargo build --release --target=x86_64-unknown-linux-musl"
  cp ${RELEASE_PATH}/signature-server-rust  ${PACKAGE_PREFIX}-bin/data-api/api-infra.signature-server-rust
echo " [*] 2. package date-api/api-infra.meta-server-java..."
  if [ ! -e data-api/api-infra/meta-server-java/target/meta-server-java-1.0-$UBERJAR ]; then
    bash -c "cd data-api/api-infra/meta-server-java && mvn assembly:assembly -DskipTests"
  else
    echo "    Finished, previous build exist, reused!"
  fi
  cp data-api/api-infra/meta-server-java/target/meta-server-java-1.0-$UBERJAR ${PACKAGE_PREFIX}-bin/data-api/api-infra.meta-server-java.jar

# echo " [TODO] 3. package date-infra.quality-server-rust ..."
# echo " [TODO] 4. package date-infra.scheduler-server-rust ..."

echo " [*] >>-------- packaging date-api/api-rpc --------"
echo " [*] 1. package date-api/api-rpc.query-server-rust ..."
  bash -c "cd data-api/api-rpc/query-server-rust && cargo build --release --target=x86_64-unknown-linux-musl"
  cp ${RELEASE_PATH}/query-server-rust  ${PACKAGE_PREFIX}-bin/data-api/api-rpc.query-server-rust


echo " [*] >>-------- packaging date-api/api-proxy --------"
echo " [*] 1. package date-api/api-proxy.postgrest-server-shell ..."
  cp -r data-api/api-proxy/postgrest-server-shell/run.sh  ${PACKAGE_PREFIX}-bin/data-api/api-proxy.postgrest-server-shell.sh

echo " [*] 2. package date-api/api-proxy.hdfs-minio-server-shell ..."
  cp -r data-api/api-proxy/hdfs-minio-server-shell/run.sh  ${PACKAGE_PREFIX}-bin/data-api/api-proxy.hdfs-minio-server-shell.sh
# echo " [*] 3. package date-api/api-proxy.buffer-minio-server-shell ..."

echo " [*] 3. package date-api/api-proxy.metabase-server-shell ..."
  cp -r data-api/api-proxy/metabase-server-shell/run.sh  ${PACKAGE_PREFIX}-bin/data-api/api-proxy.metabase-server-shell.sh
  cp -r data-api/api-proxy/metabase-server-shell/run.d   ${PACKAGE_PREFIX}-bin/data-api/api-proxy.metabase-server-shell.d



echo -ne "\n\n\n\n"
echo " [*] ======== packaging date-exchange ========"
mkdir -p ${PACKAGE_PREFIX}-bin/data-exchange

echo " [*] >>-------- packaging date-exchange/exchange-agent --------"
echo " [*] 1. package date-exchange/exchange-agent.push-agent-shell ..."
  cp -r data-exchange/exchange-agent/push-agent-shell/run.sh  ${PACKAGE_PREFIX}-bin/data-exchange/exchange-agent.push-agent-shell.sh
  cp -r data-exchange/exchange-agent/push-agent-shell/run.d   ${PACKAGE_PREFIX}-bin/data-exchange/exchange-agent.push-agent-shell.d

echo " [*] >>-------- packaging date-exchange/exchange-gate --------"
echo " [*] 1. package date-exchange/exchange-gate.push-gate-java ..."
  if [ ! -e data-exchange/exchange-gate/push-gate-java/target/push-gate-java-1.0-$UBERJAR ]; then
    bash -c "cd data-exchange/exchange-gate/push-gate-java && mvn assembly:assembly -DskipTests"
  else
    echo "    Finished, previous build exist, reused!"
  fi
  cp data-exchange/exchange-gate/push-gate-java/target/push-gate-java-1.0-$UBERJAR ${PACKAGE_PREFIX}-bin/data-exchange/exchange-gate.push-gate-java.jar
echo " [*] 2. package date-exchange/exchange-gate.aliyun-gate-rust ..."
  bash -c "cd data-exchange/exchange-gate/aliyun-gate-rust && cargo build --release --target=x86_64-unknown-linux-musl"
  cp ${RELEASE_PATH}/aliyun-gate-rust ${PACKAGE_PREFIX}-bin/data-exchange/exchange-gate.aliyun-gate-rust

# echo " [*] >>-------- packaging date-exchange/exchange-channel --------"
# echo " [*] package date-exchange/exchange-channel.dc-push-rust ..."
#   bash -c "cd data-exchange/exchange-channel/dc-push-rust && cargo build --release --target=x86_64-unknown-linux-musl"
#   cp data-exchange/exchange-channel/dc-push-rust/${RELEASE_PATH}/dc-push-rust ${PACKAGE_PREFIX}-bin/data-exchange/exchange-channel.dc-push-rust


echo -ne "\n\n\n\n"
echo " [*] ======== packaging date-pipeline========"
mkdir -p ${PACKAGE_PREFIX}-bin/data-pipeline

echo " [*] >>-------- packaging date-pipeline/pipeline-online --------"
echo " [*] 1. package date-pipeline/pipeline-online.online-server-rust ..."
  bash -c "cd data-pipeline/pipeline-online/online-server-rust && cargo build --release --target=x86_64-unknown-linux-musl"
  cp ${RELEASE_PATH}/online-server-rust  ${PACKAGE_PREFIX}-bin/data-pipeline/pipeline-online.online-server-rust

echo " [*] package date-pipeline/pipeline-online.online-server-java ..."
  BUILD_JAR=data-pipeline/pipeline-online/online-server-java/target/online-server-java-1.0-$UBERJAR
  if [ ! -e $BUILD_JAR ]; then
      bash -c "cd data-pipeline/pipeline-online/online-server-java && mvn compile assembly:assembly -DskipTests"
  else
      echo "    Finished, previous build exist, reused!"
  fi
  cp $BUILD_JAR ${PACKAGE_PREFIX}-bin/data-pipeline/pipeline-online.online-server-java.jar

echo " [*] package data-pipeline/pipeline-online.enum-server-java ..."
  BUILD_JAR=data-pipeline/pipeline-online/enum-server-java/target/enum-server-java-1.0-$UBERJAR
  if [ ! -e $BUILD_JAR ]; then
      bash -c "cd data-pipeline/pipeline-online/enum-server-java && mvn compile assembly:assembly -DskipTests"
  else
      echo "    Finished, previous build exist, reused!"
  fi
  cp $BUILD_JAR ${PACKAGE_PREFIX}-bin/data-pipeline/pipeline-online.enum-server-java.jar

echo " [*] >>-------- packaging date-pipeline/pipeline-streaming--------"
echo " [*] package date-pipeline/pipeline-streaming.flink-deps..."
  BUILD_JAR=data-pipeline/pipeline-streaming/flink-deps/target/flink-deps-1.0.jar
  if [ ! -e $BUILD_JAR ]; then
      bash -c "cd data-pipeline/pipeline-streaming/flink-deps && mvn compile package -DskipTests"
  else
      echo "    Finished, previous build exist, reused!"
  fi
  cp $BUILD_JAR ${PACKAGE_PREFIX}-bin/data-pipeline/pipeline-streaming.flink-deps.jar

echo " [*] package date-pipeline/pipeline-streaming.flink-etls..."
  BUILD_JAR=data-pipeline/pipeline-streaming/flink-etls/target/flink-etls-1.0.jar
  if [ ! -e $BUILD_JAR ]; then
      bash -c "cd data-pipeline/pipeline-streaming/flink-etls && mvn compile package -DskipTests"
  else
      echo "    Finished, previous build exist, reused!"
  fi
  cp $BUILD_JAR ${PACKAGE_PREFIX}-bin/data-pipeline/pipeline-streaming.flink-etls.jar
 



echo -ne "\n\n\n\n"
echo " [*] ======== assemble data-engine-bin tarball ========"
echo " [*]  copy binary to release ${PACKAGE_PREFIX}-bin directory"
cp data-ops/data-engine.sh ${PACKAGE_PREFIX}-bin

cp -r meta-lib-static ${PACKAGE_PREFIX}-bin && rm -rf ${PACKAGE_PREFIX}-bin/meta-lib-static/env/.env
if [ "${env}" != "dev" ]; then
  REMOTE_DOTENV="https://glb.chinapex.com.cn/api/v4/projects/251/repository/files/${env}%2Eenv/raw?ref=master"
  curl -H "PRIVATE-TOKEN: o_pPxbneRu1c4TsCGAoR" -g "$REMOTE_DOTENV" > ${PACKAGE_PREFIX}-bin/meta-lib-static/env/${env}.env
fi
bash -c "cd ${PACKAGE_PREFIX}-bin/meta-lib-static/env && ln -s ${env}.env .env"

echo " [*]  generate the ${PACKAGE_PREFIX}-bin.tar.gz..."
cd release && tar -czvf ${PACKAGE_NAME}-bin.tgz ${PACKAGE_NAME}-bin

echo " [ok] ======== tarball: ${PACKAGE_PREFIX}-bin.tgz ========"
 
date

